package com.sky.controller.admin;

import com.aliyuncs.exceptions.ClientException;
import com.sky.result.Result;
import com.sky.utils.AliOss;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api("公共接口")
public class CommonController {
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload( MultipartFile file) throws IOException, ClientException {
        log.info("上传文件至阿里云,文件名：{}", file.getOriginalFilename());
        String filename = file.getOriginalFilename();//获取文件原名
        String suffix = null;
        if (filename != null) {
            suffix = filename.substring(filename.lastIndexOf("."));//截取文件后缀
        }
        String name = UUID.randomUUID() + suffix;//使用uuid拼接后缀生成新文件名
        String url = AliOss.uploadFile(name, file.getInputStream());//上传文件至阿里云并返回url
        return Result.success(url);
    }

}
